package xyz.fivefaith.tool.mybatis.service;

/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * <p>Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * <p>http://www.gnu.org/licenses/lgpl.html
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

import lombok.Getter;

import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;

import xyz.fivefaith.tool.mybatis.domain.base.FaithBaseEntity;
import xyz.fivefaith.tool.mybatis.mapper.FaithBaseMapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 业务封装基础类
 *
 * @param <M> mapper
 * @param <T> domain
 * @author Chill
 */
@Getter
@Validated
public class FaithBaseServiceImpl<M extends FaithBaseMapper<T>, T extends FaithBaseEntity<?>>
        extends ServiceImpl<M, T> implements FaithBaseService<T> {

    /** ========================== insert/delete/update/delete ========================== */
    @Override
    public boolean alwaysUpdateSomeColumnById(T entity) {
        return baseMapper.alwaysUpdateSomeColumnById(entity) > 0;
    }

    @Override
    public boolean alwaysUpdate(Wrapper<T> updateWrapper) {
        return baseMapper.alwaysUpdate(null, updateWrapper) > 0;
    }

    @Override
    public T alwaysGetById(Serializable id) {
        return baseMapper.alwaysSelectById(id);
    }

    @Override
    public List<T> alwaysListBatchIds(Collection<? extends Serializable> idList) {
        return baseMapper.alwaysSelectBatchIds(idList);
    }

    @Override
    public T alwaysSelectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
        if (throwEx) {
            return baseMapper.alwaysSelectOne(queryWrapper);
        }
        return SqlHelper.getObject(log, baseMapper.alwaysSelectList(queryWrapper));
    }

    @Override
    public Long alwaysSelectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return baseMapper.alwaysSelectCount(queryWrapper);
    }

    @Override
    public List<T> alwaysSelectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return baseMapper.alwaysSelectList(queryWrapper);
    }
}
